﻿using __Able.Network.Utils;
using __Able.Core.Utilities;
using System;

namespace CCU.Packet
{
    public class KeepAlivePacket : CCUPacket
    {
        /// <summary>
        /// 
        /// </summary>
        public static readonly string module = "base";
        /// <summary>
        /// 
        /// </summary>
        public static readonly byte[] version = { 1, 0, 0, 0 };
        /// <summary>
        /// 
        /// </summary>
        public static readonly ushort token = 0x0001;
        /// <summary>
        /// 
        /// </summary>
        public static readonly byte packetTail = 0xff;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <param name="id"></param>
        /// <param name="sequence"></param>
        public KeepAlivePacket(User user, byte[] id, byte sequence = 0)
            : base(module, version, token, sequence, id, user, packetTail)
        {

        }
        protected override void PutHead(ByteBuffer buf)
        {
            buf.Put(this.Sequence);
            buf.Put(this.ID);
            buf.Put(this.Version);
            buf.PutUShort(this.Token);
            buf.Put(Util.GetBytes(this.Module));
        }
        protected override void PutBody(ByteBuffer buf)
        {
            buf.Put(Util.GetBytes(DNTRequest.GetIP()));
        }
        protected override void PutTail(ByteBuffer buf)
        {
            buf.Put(this.PacketTail);
        }

        protected override void ParseHeader(ByteBuffer buf)
        {
            int hl = buf.GetUShort();
            int pos = buf.Position;
            Console.WriteLine("HeaderLength:\t{0}", hl);
            Console.WriteLine("Sequence:\t{0}", buf.Get());
            Console.WriteLine("ID:\t\t{0}", new Guid(buf.GetByteArray(16)).ToString());
            Console.WriteLine("Version:\t{0}", BitConverter.ToString(buf.GetByteArray(4)));
            Console.WriteLine("Token:\t\t{0}", buf.GetUShort());
            Console.WriteLine("ModuleName:\t{0}", Util.GetString(buf.GetByteArray(hl + pos - buf.Position)));
        }

        protected override void ParseBody(ByteBuffer buf)
        {
            Console.WriteLine("Body:\t\t{0}", Util.GetString(buf.GetByteArray(buf.Length - buf.Position - 1)));
        }

        protected override void ParseTail(ByteBuffer buf)
        {
            Console.WriteLine("Tail:\t\t{0}", buf.Get());
        }
    }
}
